import { clipPlayer } from "../../clip.js";
import { clipAfterimage } from "../../clipAfterimage.js";
import sceneContainer from "../../scene/container.js";
import share from "../../share.js";
export async function init(Info) {
  this.Info = Info

  this.clockMap[4000] = share.getClock(4000);

  await this.load();
  this.weaponInfo = this.resource.Content.Character.Weapon[this.Info.weaponId].info;
  this.afterImage = this.weaponInfo.afterImage;
  this.afterImageAni = {
    stabO1: null,
    stabO2: null,
    swingO1: null,
  };
  // 将每种行为的PIXI.AnimationSprite切出来
  for (let key in this.performance) {
    this.performance[key] = await clipPlayer(this.resource, this.Info, key);
    // 由于每个PIXI.Texture的anchor不同，所以这里必须设置为true，才能生效
    this.performance[key].updateAnchor = true;
    this.performance[key].animationSpeed = 0.04;
  }

  // 取消攻击动作的循环
  this.performance.stabO1.loop = false;
  this.performance.stabO2.loop = false;
  this.performance.swingO1.loop = false;

  // 将每种攻击效果的PIXI.AnimationSprite切出来
  for (let key in this.afterImageAni) {
    this.afterImageAni[key] = await clipAfterimage(
      this.resource,
      this.afterImage,
      key
    );
    this.afterImageAni[key].ani.animationSpeed = 0.33;
    this.afterImageAni[key].ani.loop = false;
  }

  // 处理攻击AnimationSprite切换Texture的回调，以及结束的回调
  setFrameChange.call(this);
  setFrameComplete.call(this);

  // 初始化动作
  this.animationSprite = this.performance.stand1;

  // 测试点
  window.player = this;

  // 初始化位置
  this.container.x = this.position.x;
  this.container.y = this.position.y;
  this.container.addChild(this.animationSprite);

  // 定时函数
  this.clockMap[4000].addEvent(() => {
    this.recover();
  });
  this.clockMap[4000].run();

  // 交互
  interactive.call(this);
}

function setFrameChange() {
  this.performance.stabO1.onFrameChange = (num) => {
    if (num === 1) {
      this.afterImageAni.stabO1.ani.gotoAndPlay(0);
      this.container.addChild(this.afterImageAni.stabO1.ani);
      this.attack();
    }
  };

  this.performance.stabO2.onFrameChange = (num) => {
    if (num === 1) {
      this.afterImageAni.stabO2.ani.gotoAndPlay(0);
      this.container.addChild(this.afterImageAni.stabO2.ani);
      this.attack();
    }
  };

  this.performance.swingO1.onFrameChange = (num) => {
    if (num === 2) {
      this.afterImageAni.swingO1.ani.gotoAndPlay(0);
      this.container.addChild(this.afterImageAni.swingO1.ani);
      this.attack();
    }
  };
}

function setFrameComplete() {
  this.performance.stabO1.onComplete = () => {
    this.attackLock = false;
    this.container.removeChild(this.afterImageAni.stabO1.ani);

    this.stand1();
  };

  this.performance.stabO2.onComplete = () => {
    this.attackLock = false;
    this.container.removeChild(this.afterImageAni.stabO2.ani);
    this.stand1();
  };
  this.performance.swingO1.onComplete = () => {
    this.attackLock = false;
    this.container.removeChild(this.afterImageAni.swingO1.ani);
    this.stand1();
  };
}

function interactive() {
  this.keyboard.addTrigger((get, backupGet) => {
    if (get('up')) {
      this.direction.stopLadder();
      sceneContainer.ladderRopes.ladderRppes.forEach((item) => {
        if (item.isLadder()) {
          if (
            this.position.x > item.getX() - 10 &&
            this.position.x < item.getX() + 10
          ) {
            if (this.position.y >= item.getGoTop()) {
              this.direction.ladder();
              this.ladderRope = item;
            }
          }
        } else if (!item.isLadder()) {
          if (
            this.position.x > item.getX() - 8 &&
            this.position.x < item.getX() + 8
          ) {
            if (
              this.position.y >= item.getGoTop() &&
              this.position.y - this.getHeight() / 2 < item.getBottom()
            ) {
              this.direction.ladder();
              this.ladderRope = item;
            }
          }
        }
      });
    }
    if (get('down')) {
      this.direction.stopLadder();
      sceneContainer.ladderRopes.ladderRppes.forEach((item) => {
        if (item.isLadder()) {
          if (
            this.position.x > item.getX() - 10 &&
            this.position.x < item.getX() + 10
          ) {
            if (
              this.position.y >= item.getTop() &&
              this.position.y < item.getBottom()
            ) {
              this.direction.ladder();
              this.ladderRope = item;
            }
          }
        } else if (!item.isLadder()) {
          if (
            this.position.x > item.getX() - 8 &&
            this.position.x < item.getX() + 8
          ) {
            if (
              this.position.y >= item.getLeaveTop() &&
              this.position.y < item.getBottom()
            ) {
              if (this.position.y === item.getLeaveTop()) {
                this.position.y = item.getGoTop();
              }
              this.direction.ladder();
              this.ladderRope = item;
            }
          }
        }
      });
    }

    if (!get('left')) {
      this.direction.stopLeft();
    }
    if (!get('right')) {
      this.direction.stopRight();
    }
    if (!get('d')) {
      this.direction.stopJump();
    }
    if (!get('a')) {
      this.direction.stopAttack();
    }
    if (backupGet('up') && !get('up')) {
      this.direction.stopUp();
    }
    if (!get('down')) {
      this.direction.stopBottom();
    }

    if (!backupGet('up') && get('up')) {
      this.direction.up();
      return;
    }
    if (get('down')) {
      this.direction.bottom();
      return;
    }

    if (get('a')) {
      this.direction.attack();
      this.direction.stopLeft();
      this.direction.stopRight();
      return;
    }

    if (get('d')) {
      this.direction.jump();
    }

    if (get('left') || get('right')) {
      if (get('left') && get('right')) {
        this.direction.stopLeft();
        this.direction.stopRight();
      } else {
        if (get('right')) {
          this.direction.right();
        } else if (get('left')) {
          this.direction.left();
        }
      }
      if (!get('d')) {
        this.direction.walk();
      }
    }

    if (!get('a') && !get('d') && !get('left') && !get('right')) {
      this.direction.walk();
    }
  })
  
}
